package com.invengo.module_check.ui.fragment.widget;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.LayoutInflater;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.device.DeviceHelper;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.trello.rxlifecycle2.LifecycleProvider;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

public class AnalyzeTestDialog {
    public AnalyzeTestViewModel viewModel;
    private static AnalyzeTestDialog instance;
    private ViewDataBinding binding;
    private MaterialDialog dialog;

    public static AnalyzeTestDialog getInstance() {
        if (instance == null) synchronized (AnalyzeTestDialog.class) {
            if (instance == null) {
                instance = new AnalyzeTestDialog();
            }
        }
        return instance;
    }

    private AnalyzeTestDialog() {
    }

    public void showDialog(FragmentActivity activity) {
        if (dialog == null) {
            initDialog(activity);
        }
        if (ACache.getInstance().getAsBoolean(AKey.IS_LONG_PRESS_MODEL)){
            ToastUtils.showShort("标签测试不支持长按模式，请切换成短按模式");
            return;
        }
        if (!dialog.isShowing())
            dialog.show();
    }

    private void initDialog(FragmentActivity activity) {
        viewModel = ViewModelProviders.of(activity).get(AnalyzeTestViewModel.class);
        binding = DataBindingUtil.inflate(LayoutInflater.from(activity),
                R.layout.dialog_analyze_test, null, false);
        if (dialog == null)
            dialog = new MaterialDialog.Builder(activity)
                    .customView(binding.getRoot(), false)
                    .showListener(dialog -> {
                        DeviceHelper.getInstance().addTempCache(AnalyzeTestDialog.class.getSimpleName());
                        viewModel.registerEvent(AnalyzeTestDialog.class.getSimpleName());
                    })
                    .dismissListener(dialog -> {
                        viewModel.removeEvent();
                        viewModel.itemList.clear();
                    })
                    .build();
        dialog.setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == 300) {
                return activity.onKeyDown(keyCode, event);
            }
            return true;
        });
        binding.setVariable(BR.viewModel, viewModel);
        binding.setLifecycleOwner(activity);
        //让ViewModel拥有View的生命周期感应
        activity.getLifecycle().addObserver(viewModel);
        //注入RxLifecycle生命周期
        viewModel.injectLifecycleProvider((LifecycleProvider) activity);
    }
}
